/**
 * File Name   : FullColAdAction.java
 * Description : 
 */
package com.hh.admin.ad.fulcolad.adction;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.ad.fulcolad.form.FullColAdForm;
import com.hh.admin.ad.fulcolad.form.ParamFullColAdBean;
import com.hh.admin.ad.fulcolad.form.ResultFullColAdBean;
import com.hh.admin.ad.fulcolad.service.FullColAdService;
import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.common.bean.PortalLocalBean;
import com.hh.common.consts.AdminConst;

/**
 * 通栏广告管理
 * @author 冯昌硕
 * @create 2013-1-24 下午11:25:06
 * @modify 2013-02-25 添加portalLst方法查询首页要显示的滚动广告
 */
public class FullColAdAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(FullColAdAction.class);
    
    /**
     * 公共Service
     */
    private CommonService commonService;
    
    private FullColAdService fullColAdService;
    
    /**
     * 获取通栏广告内容列表
     * @create 2013-1-24 下午11:36:17
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        
        // 查询当前页要显示的记录
        ParamFullColAdBean paramBean = new ParamFullColAdBean();
        List<ResultFullColAdBean> fullColAdLst = 
                commonService.selRecordList(fForm, "fullcolad.selFullColAdList", paramBean);
        request.setAttribute("fullColAdLst", fullColAdLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "fullcolad.selFullColAdCount");
        fForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / fForm.getPageSize();
        if(totleCnt % fForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        fForm.setTotlePage(pageCnt);
        log.debug("查询通栏广告记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加记录
     * @create 2013-1-26 上午12:34:11
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        fForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = fullColAdService.insertFullColAd(fForm);
        log.debug("新增通栏广告内容ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 删除单条记录
     * @create 2013-1-27 上午10:54:03
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        
        int id = fForm.getId();
        log.debug("通栏广告内容删除，要删除的记录ID=[" + id + "]");
        int delCnt = fullColAdService.deleteOneFullColAd(id);
        log.debug("通栏广告[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-27 下午12:54:36
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        Integer[] ids = fForm.getIds();
        
        int delCnt = fullColAdService.deleteManyFullColAd(ids);
        log.debug("批量通栏广告内容删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 打开内容编辑页面
     * @create 2013-1-27 下午1:17:05
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        int id = fForm.getId();
        ResultFullColAdBean result = fullColAdService.getFullColAdDetail(id);
        
        request.setAttribute("fullColAdDetail", result);
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的内容更新
     * @create 2013-1-27 下午3:31:12
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        int updCnt = fullColAdService.updateFullColAd(fForm);
        log.debug("修改通栏广告[" + fForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * Portal页面查询使用，获取要在首页显示的滚动通栏广告内容记录
     * @create 2013-2-22 下午11:30:28
     */
    public ActionForward portalLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FullColAdForm fForm = (FullColAdForm) form;
        
        // 查询当前页要显示的记录
        ParamFullColAdBean paramBean = new ParamFullColAdBean();
        List<ResultFullColAdBean> fullColAdLst = 
                commonService.selRecordList(fForm, "fullcolad.selPortalFullColAdList", paramBean);
        request.setAttribute("fullColAdLst", fullColAdLst);
        
        return mapping.findForward("portalLst");
    }
    
    /**
     * 首页打开内容详情页面
     * @create 2013-3-05 00:00
     */
    public ActionForward detailPortal(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("首页");
        firLocalBean.setHref("./idx.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        List<PortalLocalBean> afterLocalLst = new ArrayList<PortalLocalBean>();
        PortalLocalBean afterLocalBean = new PortalLocalBean();
        afterLocalBean.setShowName("广告");
        afterLocalBean.setHref("#");
        afterLocalLst.add(afterLocalBean);
        request.setAttribute("afterLocalLst", afterLocalLst);
        
        FullColAdForm fForm = (FullColAdForm) form;
        int id = fForm.getId();
        ResultFullColAdBean result = fullColAdService.getFullColAdDetail(id);
        
        request.setAttribute("fullColAdDetail", result);
        return mapping.findForward("detailPage");
    }

    public void setFullColAdService(FullColAdService fullColAdService) {
        this.fullColAdService = fullColAdService;
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }
    
}
